import { cloneDeep } from 'lodash-es'
import { http } from '@/utils/http'
import { ElMessage, ElMessageBox } from 'element-plus'
// import { useHomeStore } from './home-store.js'

import _ from 'lodash-es'



export const useDialogStore = () => {
    const router = useRouter()
    // const homestore = useHomeStore()
    const languageJson = inject('languageJson')
    const languageType = inject('languageType')
    const state = reactive({
        matchListLoading: false,
        dialogArr: [],
        orderConfirm: {
            show: false,
            data: {}
        },
        orderData: {
            show: false,
            data: {}
        },
        textmap: {
            soccer_handicap: {
                ce_odds_data: languageJson.value.home.corner,
                odds_data: '',
                pc_odds_data: languageJson.value.home.card,

                full: languageJson.value.home.full,
                half: languageJson.value.home.half,

                M: '',
                R: languageJson.value.home.letballgo,
                OU: languageJson.value.home.ou,
                OUH: languageJson.value.home.homeGoalTotal,
                OUC: languageJson.value.home.awayGoalTotal,
                CRS: ''
            },
            basketball_handicap: {
                ce_odds_data: languageJson.value.home.corner,
                odds_data: '',
                pc_odds_data: languageJson.value.home.card,
                // pd_odds_data: '波胆',

                full: languageJson.value.home.full,
                half: languageJson.value.home.half,

                section1: languageJson.value.home.section1,
                section2: languageJson.value.home.section2,

                M: '',
                R: languageJson.value.home.letballgo,
                OU: languageJson.value.home.ou,
                OUH: languageJson.value.home.basketballFirstScore,
                OUC: languageJson.value.home.basketballSecondScore,
                CRS: ''
            },
        }
    })

    const openNewDialog = (match, pk_odds_key, pk_ratio, pk_ratio2, index) => {
        state.matchListLoading = true
        let body = {
            bc_type: "hga",
            event_type: match.event_type,
            match_id: match.match_id,
            ids: match.ids,
            sport_type: match.sport_type
        }
        let handicap = match.sport_type == "soccer" ? state.textmap.soccer_handicap : state.textmap.basketball_handicap

        http.post('/match/get_now_odds_all_details', body).then((res) => {
            let pkoddskeyarr = pk_odds_key.split(':');

            let a = {}
            let handicapname = []

            if (pkoddskeyarr[0] == 'C') {
                handicapname.push(`${handicap[[pkoddskeyarr[1]]]}${languageJson.value.home.corner}`)
                if (pkoddskeyarr[2] == 'OU') {
                    if (pkoddskeyarr[pkoddskeyarr.length - 1] == '0') {
                        handicapname.push(languageJson.value.home.over)
                    } else {
                        handicapname.push(languageJson.value.home.under)
                    }
                } else {
                    handicapname.push(handicap[[pkoddskeyarr[2]]])
                }
                a = res.data.ce_odds_data[pkoddskeyarr[1]][pkoddskeyarr[2]]
            } else if (pkoddskeyarr[0] == 'F') {
                handicapname.push(`${handicap[[pkoddskeyarr[1]]]}${languageJson.value.home.card}`)
                if (pkoddskeyarr[2] == 'OU') {
                    if (pkoddskeyarr[pkoddskeyarr.length - 1] == '0') {
                        handicapname.push(languageJson.value.home.over)
                    } else {
                        handicapname.push(languageJson.value.home.under)
                    }
                } else {
                    handicapname.push(handicap[[pkoddskeyarr[2]]])
                }
                a = res.data.pc_odds_data[pkoddskeyarr[1]][pkoddskeyarr[2]]
            } else {
                handicapname.push(`${handicap[[pkoddskeyarr[0]]]}`)

                if (pkoddskeyarr[1] == 'OU') {
                    if (pkoddskeyarr[pkoddskeyarr.length - 1] == '0') {
                        handicapname.push(languageJson.value.home.over)
                    } else {
                        handicapname.push(languageJson.value.home.under)
                    }
                } else {
                    if (pkoddskeyarr[1] == 'R') {
                        if (pkoddskeyarr[pkoddskeyarr.length - 1] == '0') {
                            handicapname.push(languageJson.value.home.home)
                        } else {
                            handicapname.push(languageJson.value.home.away)
                        }
                    }
                    if (pkoddskeyarr[1] != 'M') {
                        handicapname.push(handicap[pkoddskeyarr[1]])
                    } else {
                        if (pkoddskeyarr[3] == '0') {
                            handicapname.push([`${languageJson.value.home.home}${languageJson.value.home.win}`])
                        }
                        if (pkoddskeyarr[3] == '1') {
                            handicapname.push([`${languageJson.value.home.away}${languageJson.value.home.win}`])
                        }
                        if (pkoddskeyarr[3] == '2') {
                            handicapname.push([languageJson.value.home.draw])
                        }
                    }

                    if (pkoddskeyarr[1] == 'OUH' || pkoddskeyarr[1] == 'OUC') {
                        if (pkoddskeyarr[pkoddskeyarr.length - 1] == '0') {
                            handicapname.push(languageJson.value.home.over)
                        } else {
                            handicapname.push(languageJson.value.home.under)
                        }
                    }


                }
                a = res.data.odds_data[pkoddskeyarr[0]][pkoddskeyarr[1]]
            }

            let c = {}
            if (pkoddskeyarr[0] == 'C' || pkoddskeyarr[0] == 'F') {
                a.forEach((item) => {
                    if (item.pk_ratio == pk_ratio) {
                        c = item
                    }
                })
            } else {
                a.forEach((item) => {
                    if (item.pk_ratio == pk_ratio) {
                        c = item
                    }
                })
            }


            let b = {
                index: state.dialogArr.length + 1,
                show: true,
                position: { x: 100 + state.dialogArr.length * 20, y: 100 + state.dialogArr.length * 20 },
                zindex: 2000 + state.dialogArr.length,
                dragging: false,
                offsetX: 0,
                offsetY: 0,
                ref: null,
                match: match,
                ids: match.ids,
                pkoddskeyarr: pkoddskeyarr,
                // pk_key1: pk_key1,
                // pk_key2: pk_key2,
                pkindex: index,
                pk_ratio: pk_ratio,
                handicapname: handicapname.join(':'),
                activeodds: [
                    {
                        pk_ratio: pk_ratio,
                        index: 1,
                        otherplat: [],
                        data: c,
                        order: []
                    }
                ],
                data: a
            }


            getotherplatformoddlist(b, 0)


        }).catch((error) => {


            state.matchListLoading = false
        })
    }

    const openNewDialog2 = (row) => {
        console.log(row);

    }

    const getotherplatformoddlist = (row, index) => {

        let body = {
            bc_type: "hga",
            event_type: row.match.event_type,
            league_id: row.match.league_id,
            home_id: row.match.home_id,
            away_id: row.match.away_id,
            pk_odds_key: row.activeodds[index].data.odds[row.pkindex].pk_odds_key,
            nav_data: row.activeodds[index].data.odds[row.pkindex].nav_data,
            odd: row.activeodds[index].data.odds[row.pkindex].odd,
            sport_type: row.match.sport_type
        }


        http.post('/match/other_platform_odd_list', body).then((res) => {
            let a = {
                total_stake: 0,
                place_data: []
            }
            res.data.forEach(item => {
                if (item.bc_type == 'hga') {
                    item.min_odd = item.odd
                    item.max_odd = 1000
                    a.place_data.push(item)

                }
            });
            row.activeodds[0].order.push(a)
            row.activeodds[index].otherplat = res.data
            getMemberLimit(row, 0)
        }).catch(() => {
            state.matchListLoading = false
        })

    }

    const getMemberLimit = (row, index2) => {
        let body = {}
        state.matchListLoading = false
        row.activeodds[index2].order[row.activeodds[index2].order.length - 1].limit = {
            min_stake: 0,
            max_stake: 0
        }
        state.dialogArr.push(row)
        // row.data.forEach(item => {
        //     if (item.pk_ratio == row.pk_ratio) {
        //         let odd = item.odds[row.pkindex]
        //         body = {
        //             pk_odds_key: odd.pk_odds_key,
        //             nav_data: odd.nav_data,
        //             bc_type: row.match.bc_type
        //         }
        //     }
        // });
        // http.post('/match/member_limit', body).then((res) => {
        //     row.activeodds[index2].order[row.activeodds[index2].order.length - 1].limit = res.data
        //     state.matchListLoading = false
        //     state.dialogArr.push(row)


        // }).catch(() => {
        //     state.matchListLoading = false
        // })

    }

    const clickOrderDialog = (item) => {

        let maxZIndexObject = state.dialogArr.sort((a, b) => b.zindex - a.zindex)[0];
        let maxIndex = maxZIndexObject.zindex
        let itemIndex = item.zindex

        item.zindex = maxIndex
        maxZIndexObject.zindex = itemIndex
    }

    const addactiveodds = (pk_ratio, dataindex) => {
        let item = state.dialogArr[dataindex]
        let index = item.activeodds.findIndex(item => item.pk_ratio == pk_ratio);

        if (index !== -1) {
            if (item.activeodds.length > 1) {
                item.activeodds.splice(index, 1);
            }
        } else {
            let c = {}
            item.data.forEach(item2 => {
                if (item2.pk_ratio == pk_ratio) {
                    c = item2

                }
            });
            let row = {
                pk_ratio: pk_ratio,
                index: state.dialogArr.length + 1,
                otherplat: [],
                data: c,
                order: []
            }
            let index2 = item.data.findIndex(item => item.pk_ratio == pk_ratio);
            let body = {
                bc_type: "hga",
                event_type: item.match.event_type,
                league_id: item.match.league_id,
                home_id: item.match.home_id,
                away_id: item.match.away_id,
                pk_odds_key: item.data[index2].odds[item.pkindex].pk_odds_key,
                nav_data: item.data[index2].odds[item.pkindex].nav_data,
                odd: item.data[index2].odds[item.pkindex].odd,
                sport_type: item.match.sport_type
            }

            http.post('/match/other_platform_odd_list', body).then((res) => {
                let a = {
                    total_stake: 0,
                    place_data: []
                }
                res.data.forEach(item => {
                    if (item.bc_type == 'hga') {
                        item.min_odd = item.odd
                        item.max_odd = 1000
                        a.place_data.push(item)
                    }
                });
                row.order.push(a)
                row.otherplat = res.data
                getMemberLimit2(item, row, dataindex)
            })

        }

    }

    const getMemberLimit2 = (data, row, dataindex) => {
        let body = {}
        row.order[row.order.length - 1].limit = {
            min_stake: 0,
            max_stake: 0
        }
        state.dialogArr.push(row)
        state.dialogArr[dataindex].activeodds.push(row)
    }

    const allactiveovertime = (value, index) => {
        state.dialogArr[index].activeodds.forEach(item => {
            item.order.forEach(item2 => {
                item2.place_data.forEach(item3 => {
                    item3.runtime = value
                });
            });
        });
    }

    const changeordermode = (orderdata, data2, data3,orderindex, value) => {
        orderdata.place_data[orderindex].place_mode = ''
        let body = {
            pk_odds_key: data2.pk_odds_key,
            nav_data: data2.nav_data,
            bc_type: orderdata.place_data[0].bc_type
        }
        http.post('/match/member_limit', body).then((res) => {
            orderdata.limit = res.data
        })

        // if (value == 'count_user') {
        //     http.get(`/order/get_place_members_count?bc_type=${orderdata.place_data[0].bc_type}`).then((res) => {
        //         if (res.data.member_count >= 2) {
        //             orderdata.alluser = {
        //                 member: res.data.member_count,
        //                 member_count: res.data.member_count >= 2 ? 2 : 1,
        //                 disabled: res.data.members_count >= 2 ? false : true,
        //                 show: true
        //             }
        //         } else {
        //             orderdata.place_data[orderindex].order_mode = 'one_user'
        //         }


        //     })
        // } else {
        //     orderdata.alluser = {
        //         member_count: 1,
        //         show: false
        //     }
        // }




    }

    const gethandicapname = (sport_type, pk_odds_key) => {
        let pkoddskeyarr = pk_odds_key.split(':');
        let handicap = sport_type == "soccer" ? state.textmap.soccer_handicap : state.textmap.basketball_handicap
        let a = {}
        let handicapname = []
        let handicapnamestr = ''
        if (pkoddskeyarr[0] == 'C') {
            handicapname.push(`${handicap[[pkoddskeyarr[1]]]}${languageJson.value.home.corner}`)
            if (pkoddskeyarr[2] == 'OU') {
                if (pkoddskeyarr[pkoddskeyarr.length - 1] == '0') {
                    handicapname.push(languageJson.value.home.over)
                } else {
                    handicapname.push(languageJson.value.home.under)
                }
            } else {
                handicapname.push(handicap[[pkoddskeyarr[2]]])
            }
        } else if (pkoddskeyarr[0] == 'F') {
            
            handicapname.push(`${handicap[[pkoddskeyarr[1]]]}${languageJson.value.home.card}`)
            if (pkoddskeyarr[2] == 'OU') {
                if (pkoddskeyarr[pkoddskeyarr.length - 1] == '0') {
                    handicapname.push(languageJson.value.home.over)
                } else {
                    handicapname.push(languageJson.value.home.under)
                }
            } else {
                handicapname.push(handicap[[pkoddskeyarr[2]]])
            }
        } else {
            handicapname.push(`${handicap[[pkoddskeyarr[0]]]}`)

            if (pkoddskeyarr[1] == 'OU') {
                if (pkoddskeyarr[pkoddskeyarr.length - 1] == '0') {
                    handicapname.push(languageJson.value.home.over)
                } else {
                    handicapname.push(languageJson.value.home.under)
                }
            } else {

                if (pkoddskeyarr[1] == 'R') {
                    if (pkoddskeyarr[pkoddskeyarr.length - 1] == '0') {
                        handicapname.push(languageJson.value.home.home)
                    } else {
                        handicapname.push(languageJson.value.home.away)
                    }
                }
                if (pkoddskeyarr[1] != 'M') {
                    handicapname.push(handicap[pkoddskeyarr[1]])
                } else {
                    if (pkoddskeyarr[3] == '0') {
                        handicapname.push([`${languageJson.value.home.home}${languageJson.value.home.win}`])
                    }
                    if (pkoddskeyarr[3] == '1') {
                        handicapname.push([`${languageJson.value.home.away}${languageJson.value.home.win}`])
                    }
                    if (pkoddskeyarr[3] == '2') {
                        handicapname.push([languageJson.value.home.draw])
                    }
                }
                if (pkoddskeyarr[1] == 'OUH' || pkoddskeyarr[1] == 'OUC') {
                    if (pkoddskeyarr[pkoddskeyarr.length - 1] == '0') {
                        handicapname.push(languageJson.value.home.over)
                    } else {
                        handicapname.push(languageJson.value.home.under)
                    }
                }


            }
        }

        if (pkoddskeyarr[1] != 'M') {
            handicapname.push(pkoddskeyarr[pkoddskeyarr.length - 2])
        }
        handicapnamestr = handicapname.join(':');
        return handicapnamestr


    }

    onMounted(async () => {
        initData()
        
    })

    onUnmounted(() => {
    })


    async function initData() {

    }


    return {
        state,

        openNewDialog,
        clickOrderDialog,
        addactiveodds,
        allactiveovertime,
        openNewDialog2,
        changeordermode,
        gethandicapname

    }
}
